CMAKE_MINIMUM_REQUIRED(VERSION 3.10 FATAL_ERROR)

CMAKE_POLICY(SET CMP0043 NEW )
CMAKE_POLICY(SET CMP0020 NEW )

SET(BP_MAJOR_VERSION "2")
SET(BP_MINOR_VERSION "0")
SET(BP_PATCH_VERSION "3")

SET(BP_VERSION ${BP_MAJOR_VERSION}.${BP_MINOR_VERSION}.${BP_PATCH_VERSION})
SET(BP_SOVERSION ${BP_MAJOR_VERSION}${BP_MINOR_VERSION}${BP_PATCH_VERSION})

PROJECT(BPRobotSimulation)
SET(ARCH x86_64)
SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on window")
SET(CMAKE_RELEASE_POSTFIX "" CACHE STRING "add a postfix, usually empty on windows" )
SET(CMAKE_RELWITHDEBINFO_POSTFIX "rd" CACHE STRING "add a postfix, usually empty on windows")
SET(CMAKE_MINSIZEREL_POSTFIX "s" CACHE STRING "add a postfix, usually empty on windows")

#SET(CPACK_PACKAGE_VERSION "${BP_VERSION}")
#SET(CPACK_PACKAGE_VERSION_MAJOR "${BP_MAJOR_VERSION}")
#SET(CPACK_PACKAGE_VERSION_MINOR "${BP_MINOR_VERSION}")
#SET(CPACK_PACKAGE_VERSION_PATCH "${BP_PATCH_VERSION}")
#SET(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)

#SET(PROJECT_PLUGINS_DIR plugins-${BP_VERSION} CACHE STRING "set the plugin dir" )
SET(PROJECT_PLUGINS_DIR plugins CACHE STRING "set the plugin dir" )

IF ( NOT WIN32 )
    IF( APPLE )
        set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
        set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
        #set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
    ENDIF(APPLE)
    set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
    add_definitions("-Wall -g")
ELSE(NOT WIN32)
    SET(CMAKE_CXX_STANDARD 14)
ENDIF( NOT WIN32 )
#set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
#set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++14")
#set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++14 -stdlib=libc++")

#set(STD_CXX -std=gnu++14)
#set(STD_CXX -std=c++14)
#set(CMAKE_CXX_STANDARD 14)

OPTION("SELF_DEBUG" OFF)
OPTION("USE_ODBC" OFF)
OPTION(WITH_EXAMPLE "WITH EXAMPLE" OFF)
OPTION(USE_REDISDB "use RedisDB" OFF)
OPTION(USE_MONGODB "use MongoDB" OFF)

SET(CMAKE_ADDTION_LIBRARIES "" CACHE STRING "add other win system lib, splite with ' '" )

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}")

OPTION(BUILD_AT_UNICODE "build project at unicode env" "OFF")

OPTION(BUILD_WITH_PCH "Enable use of precomplied headers, experimental" OFF)

#SET( CMAKE_CXX_FLAGS "-std=c++14 -O3")
INCLUDE(UtilityMacros)
INCLUDE(PlatformConfiguration)
INCLUDE(PCHMacros)

SET(BP_SOURCE_DIR ${PROJECT_SOURCE_DIR} CACHE PATH "set project path")
IF(WIN32)
  SET(DEPEND_PATH "${BP_SOURCE_DIR}/depends/win" CACHE PATH "Set depend path at here" )
ELSE(WIN32)
  SET(DEPEND_PATH "${BP_SOURCE_DIR}/depends/linux" CACHE PATH "Set depend path at here" )
ENDIF(WIN32)

OPTION(USE_LZ4 "use lz4 data" ON)

FIND_PATH(X264_LIB_DIR libx264.so x264
  PATHS
  /usr/lib
  /usr/local/lib
  /usr/lib/x86_64-linux-gnu
  )

FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
  set(CMAKE_EXE_LINKER_FLAGS"${CMAKE_EXE_LINKER_FLAGS}${OpenMP_EXE_LINKER_FLAGS}")
endif()

OPTION(USE_CUDA "use cuda flag" OFF)
if ( USE_CUDA)
  enable_language(CUDA)
  FIND_PACKAGE(CUDA REQUIRED)
  if (CMAKE_CUDA_COMPILER)
    # Enable C++11 globally for CUDA code
    if(NOT DEFINED CMAKE_CUDA_STANDARD)
      set(CMAKE_CUDA_STANDARD 11)
      set(CMAKE_CUDA_STANDARD_REQUIRED ON)
      set(CMAKE_CUDA_FLAGS
        "${CMAKE_CUDA_FLAGS} \
    --expt-relaxed-constexpr \
  "
        )
      if(CMAKE_BUILD_TYPE MATCHES DEBUG)
        set(CMAKE_CUDA_FLAGS
          "${CMAKE_CUDA_FLAGS} \
      --generate-line-info \
      -O3 \
    "
          ) #--device-debug #everything fails at device-debug
      endif()
    endif()
  else()
    message(FATAL_ERROR "CMake did not find CUDA, which is required to build this program.")
    SET(USE_CUDA OFF)
  endif()
ENDIF(USE_CUDA)


SET(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}${BP_VERSION})

IF(NOT ${PROJECT_BINARY_DIR} EQUAL ${PROJECT_SOURCE_DIR})
  INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR}/include)
ENDIF()

SET(BP_CONFIG_HEADER "${PROJECT_BINARY_DIR}/include/BluePhysics/config.h")
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/src/config.in"
  "${BP_CONFIG_HEADER}")

SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} CACHE STRING "YOU MAY ADD ADDITIONAL SEARCH PATHS HERE. USE ; TO SEPARATE MULTIPLE PATHS.")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} CACHE STRING "YOU MAY ADD ADDITIONAL SEARCH PATHS HERE. USE ; TO SEPARATE MULTIPLE PATHS.")

SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} CACHE STRING "(EXPERIMENTAL) YOU MAY ADD ADDITIONAL SEARCH PATHS HERE. USE ; TO SEPARATE MULTIPLE PATHS.")

ADD_SUBDIRECTORY(src)
IF( WITH_EXAMPLE )
  ADD_SUBDIRECTORY(examples)
ENDIF( WITH_EXAMPLE )
